package com.forever7776.life.core.service.impl.admin;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.forever7776.life.core.common.util.ResultVoUtil;
import com.forever7776.life.core.entity.admin.AdminUser;
import com.forever7776.life.core.mapper.admin.AdminUserMapper;
import com.forever7776.life.core.service.admin.IAdminUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.forever7776.life.remote.common.vo.ResultVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 管理员用户表 服务实现类
 * </p>
 *
 * @author kz
 * @since 2018-01-18
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements IAdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    /**
     * 根据账号密码查询管理员数据
     *
     * @param username
     * @param pwd
     * @return
     */
    @Override
    public AdminUser selectOne(String username, String pwd) {
        return selectOne(new EntityWrapper<AdminUser>().eq("username", username).eq("password", pwd));
    }

    /**
     * 更新用户登录时间
     *
     * @param username
     * @return
     */
    @Override
    public ResultVO updateLoginTime(String username) {
        if (StringUtils.isEmpty(username)) {
            return ResultVoUtil.getFailResult("用户登录名为空！");
        }
        adminUserMapper.updateLoginTime(username);
        return ResultVoUtil.getSuccessResult(String.format("更新用户【{}】登录时间成功！", username));
    }

    /**
     * 更新用户登出时间
     *
     * @param username
     * @return
     */
    @Override
    public ResultVO updateLogoutTime(String username) {
        if (StringUtils.isEmpty(username)) {
            return ResultVoUtil.getFailResult("用户登录名为空！");
        }
        adminUserMapper.updateLogoutTime(username);
        return ResultVoUtil.getSuccessResult(String.format("更新用户【{}】登出时间成功！", username));
    }

}
